
package FILE_SENDING;

import GUI.PrivateChatPanel;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 * This Class Handles all The Operations need to recieve a file.
 * It connects to the panel that create the class (PrivateChatPanel).
 * @author Kalif Yaniv
 */
public class RecieveFile implements Runnable
{
     Socket socket;
    String _SourceIP=null;
    File _file;
    PrivateChatPanel _creator;
    public boolean _isReady=false;
    /**
     * Constructor for RecieveFile
     * @param creator - The creator panel (PrivateChatPanel)
     * @throws UnknownHostException
     * @throws IOException
     */
    public RecieveFile(PrivateChatPanel creator/*,String IP*/) throws UnknownHostException, IOException
    {
                    _creator = creator;
                    _creator.SendMessage("Give Me Your IP");
                     JFileChooser choosewindow = new JFileChooser();
                    int returnval = choosewindow.showSaveDialog(null);
                    //create the file in the system
                            _file = new File(choosewindow.getSelectedFile().getPath());
                            _file.createNewFile();
                            _isReady=true;
    }
    /**
     * This Method Updates the source IP of the file (The sender's IP)
     * @param IP - the IP of the sender
     */
    public void UpdateIP(String IP){_SourceIP = IP;}
    /**
     * This method Runs This Thread
     * It recieves The file fron the sender and saves it to the choosen place 
     */
    public void run()
    {

        try {
            
            do
            {
                Thread.sleep(5000);
            } while(_SourceIP==null);//Wait a little bit
            
            socket = new Socket(_SourceIP, 9999); //Open socket for the file transformation
            ////////RECIEVE The File
            _creator.RecieveControlMessage("Starting The File Sending . . . ");
            int filesize = 6022386;
            byte[] mybytearray = new byte[filesize];
            int bytesRead;
            int current = 0;
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(_file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray, 0, mybytearray.length);
            current = bytesRead;
            
            do {
                bytesRead = is.read(mybytearray, current, mybytearray.length - current);
                if (bytesRead >= 0) {
                    current += bytesRead;
                }
            } while (bytesRead > -1);
            bos.write(mybytearray, 0, current);
            bos.flush();
            bos.close();
            socket.close();
            _file.createNewFile();
            _creator.RecieveControlMessage("The File Has Been Recieved Successfully . . .");
        } catch (InterruptedException ex) {
            Logger.getLogger(RecieveFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnknownHostException ex) {
            Logger.getLogger(RecieveFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RecieveFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
